package com.kaochong.xcx.service.impl;

import com.kaochong.common.util.BeanConvertUtil;
import com.kaochong.xcx.cache.WordCache;
import com.kaochong.xcx.dao.WordMapper;
import com.kaochong.xcx.entity.Word;
import com.kaochong.xcx.exception.KaoChongException;
import com.kaochong.xcx.pojo.cache.OptionModel;
import com.kaochong.xcx.pojo.cache.WordModel;
import com.kaochong.xcx.service.WordService;
import com.kaochong.xcx.utils.DistinctUtil;
import com.kaochong.xcx.utils.MixUtil;
import com.kaochong.xcx.utils.RedisUtil;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.Comparator;
import java.util.HashSet;
import java.util.List;
import java.util.Map;
import java.util.Set;
import java.util.TreeSet;

import lombok.extern.slf4j.Slf4j;

/**
 * @author han
 * @date 2018-05-02 13:41
 */
@Service
@Slf4j
public class WordServiceImpl implements WordService {
	
	@Autowired
	WordMapper wordMapper;
	
	@Autowired
	WordCache wordCache;
	
	@Autowired
	MixUtil mixUtil;
	
	@Override
	public void resetLevelStageWords(Integer level, Integer stage, Integer number) {
		for (int i = 0; i < stage; i++) {
			resetStageWords(level, i, number);
		}
	}
	
	public void resetStageWords(Integer type, Integer stage, Integer number) {
		stage = stage + 1;
		List<Word> all = wordMapper.findByWordTypeAndStage(type, stage);
		all = all.subList(0, number);
		List<WordModel> words = BeanConvertUtil.convert(all, WordModel.class);
		for (WordModel word : words) {
			word.setEnSex(word.getEnSex().trim());
		}
		if (words.size() >= number) {
			wordCache.delByStage(type, stage);
			wordCache.setStageWord(type, stage, words);
		} else {
			throw new KaoChongException(-1, "该阶段单词数量不足" + number + "个。");
		}
	}
	
	@Override
	public void resetEnSex(Integer wordType) {
		List<String> enSexList = wordMapper.selectWordSex(wordType);
		for (String s : enSexList) {
			List<OptionModel> modelList = new ArrayList<>();
			List<Word> list = wordMapper.findByWordTypeAndEnSex(wordType, s);
			list = removeDuplicateWord(list);
			
			Set<String> zhSet = new HashSet<>();
			for (Word word : list) {
				OptionModel model = new OptionModel();
				model.setId(word.getId());
				model.setZh(word.getZh());
				if (zhSet.contains(word.getZh())) {
					continue;
				}
				boolean distinct = DistinctUtil.distinct(zhSet, word.getZh());
				if (distinct) {
					continue;
				}
				zhSet.add(word.getZh());
				modelList.add(model);
			}
			if (modelList.size() >= 4) {
				log.info("{}级别词性:{}有{}个。",wordType, s, modelList.size());
				wordCache.delBySexKey(s, wordType);
				wordCache.setSexWord(s, wordType, modelList);
			} else {
				throw new KaoChongException(-1, "重置词性失败，" + s + "：数量不足4个");
			}
		}
	}
	
	/**
	 * 去重处理
	 * @param list
	 * @return
	 */
	private ArrayList<Word> removeDuplicateWord(List<Word> list) {
		Set<Word> set = new TreeSet<>(new Comparator<Word>() {
			@Override
			public int compare(Word o1, Word o2) {
				//字符串,则按照asicc码升序排列
				return o1.getEn().compareTo(o2.getEn());
			}
		});
		set.addAll(list);
		return new ArrayList<>(set);
	}
	
	@Override
	public void resetWordByLevel(Integer lever) {
		List<Word> all = wordMapper.selectByWordType(lever);
		List<WordModel> words = BeanConvertUtil.convert(all, WordModel.class);
		for (WordModel word : words) {
			word.setEnSex(word.getEnSex().trim());
		}
		if (words.size() > 0) {
			wordCache.delByType(lever + "");
			wordCache.setTypeWord(lever, words);
		}
	}
	
	@Autowired
	RedisUtil redisUtil;
	
	@Override
	public List<WordModel> stageWords(Integer target, Integer stage) {
		List<WordModel> stageList = wordCache.getStageWord(target, stage);
		Map<String, Object> map = mixUtil.mixUp(stageList, target);
		
		List<WordModel> wordList = (List<WordModel>) map.get("wordList");
		return wordList;
	}
}
